package com.zhuiYun.project.api.trend.service.Impl;

import com.zhuiYun.project.api.trend.entity.*;
import com.zhuiYun.project.api.trend.mapper.*;
import com.zhuiYun.project.api.trend.service.TrendsService;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.UploadFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName TrendsServiceImpl
 * @Description TODO 动态实体类（uniapp动态）
 * @Author gpd
 * @Date 2023-04-07 下午 04:44
 **/
@Service
public class TrendsServiceImpl implements TrendsService {

    @Autowired
    TrendsMapper trendsMapper;

    /**
     *动态内容mapper
     */
    @Autowired
    TrendContentMapper trendContentMapper;


    /**
     * 动态评论mapper
     */
    @Autowired
    TrendCommentMapper trendCommentMapper;

    /**
     * 动态内容点赞mapper
     */
    @Autowired
    TrendsThumbMapper trendsThumbMapper;

    /**
     * 动态评论点赞mapper
     */
    @Autowired
    TrendCommentThumbMapper trendCommentThumbMapper;

    /**
     * @Author gpd
     * @Description 动态上传图片和视频（用于uniapp动态）
     * @Date 2023-04-08 上午 09:37
     * @Return CommonResult
     * @param  file
     */
    @Override
    public CommonResult uploadImageAndVideo(MultipartFile file) {
        String fileId = UploadFileUtils.uploadFile(file);
        return CommonResult.ok(fileId);
    }

    /**
     * @Author gpd
     * @Description 新增用户发布的动态信息
     * @Date 2023-04-08 上午 10:18
     * @Return CommonResult

     */
    @Override
    public Integer saveUserTrends(TrendContent trendContent) {
        trendContent.setDelFlag(0);
        trendContent.setLikes(0);
        trendContent.setCreateTime(new Date());
        return trendContentMapper.insertSelective(trendContent);
    }

    /**
     * @Author gpd
     * @Description 查询所有用户发布的动态信息（用于uniapp动态）
     * @Date 2023-04-10 上午 11:43
     * @Return CommonResult
     * @param userId
     */
    @Override
    public CommonResult getUserTrend(Integer userId,Integer trendId) {
        List<TrendComment> tr = trendCommentMapper.getUserTrend(userId,trendId);
        List<TrendComment> list = new ArrayList<>();
        for (TrendComment trendComment : tr){
            if(trendComment.getParentId()==0){
                list.add(getChild(trendComment,tr));
            }
        }
        return CommonResult.ok(list);
    }



    /**
     * @Author gpd
     * @Description 新增一级动态评论（uniapp动态）
     * @Date 2023-04-10 下午 07:23
     * @Return
     * @param
     */
    @Override
    public Integer saveFirstTrend(TrendComment trendComment) {
        trendComment.setDelFlag(0);
        trendComment.setCreateTime(new Date());
        trendComment.setLikes(0);
        return trendCommentMapper.insertSelective(trendComment);
    }

    /**
     * @Author gpd
     * @Description 新增二级动态评论（uniapp动态）
     * @Date 2023-04-11 下午 01:47
     * @Return com.zhuiYun.project.common.response.CommonResult

     */
    @Override
    public Integer saveSecondTrend(TrendComment trendComment) {
        trendComment.setDelFlag(0);
        trendComment.setCreateTime(new Date());
        trendComment.setLikes(0);
        return trendCommentMapper.insertSelective(trendComment);
    }

    /**
     * @Author gpd
     * @Description 查询用户点赞表 根据用户id和动态id (uniapp 动态点赞查询)
     * @Date 2023-04-11 下午 07:31
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param tId
     * @param uId
     */
    @Override
    public List<TrendThumb> getTrendThumbByUserIdAndTrendId(Integer tId, Integer uId) {
        Example example = new Example(TrendThumb.class);
        example.createCriteria().andEqualTo("trendId",tId).andEqualTo("userId",uId);
        List<TrendThumb> list = trendsThumbMapper.selectByExample(example);
        trendsThumbMapper.updateLikeById(tId);
        return list;
    }

    /**
     * @Author gpd
     * @Description 删除点赞表中点赞信息 (uniapp 动态点赞）
     * @Date 2023-04-11 下午 07:42
     * @Return com.zhuiYun.project.common.response.CommonResult
     */
    @Override
    public Integer noThumbsUpState(TrendThumb trendThumb) {
        trendsThumbMapper.updateLikeById(trendThumb.getTrendId());
        TrendThumb trendThumb1 = trendsThumbMapper.selectOne(trendThumb);
        return trendsThumbMapper.delete(trendThumb1);
    }

    /**
     * @Author gpd
     * @Description 用户点赞 （uniapp）
     * @Date 2023-04-11 下午 07:57
     * @Return com.zhuiYun.project.common.response.CommonResult

     */
    @Override
    public Integer ThumbsUpState(TrendThumb trendThumb) {
        Integer tId = trendThumb.getTrendId();
        trendsMapper.updateThumbsUpState(tId);
        return trendsThumbMapper.insertSelective(trendThumb);
    }

    /**
     * @Author gpd
     * @Description 删除动态一级评论和二级评论（uniapp动态）
     * @Date 2023-04-12 上午 09:20
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param  id
     */
    @Override
    public Integer deleteTrendById(Integer id) {
        TrendComment trends = new TrendComment();
        trends.setDelFlag(1);
        trends.setId(id);
        return trendCommentMapper.updateByPrimaryKeySelective(trends);
    }

    /**
     * @Author gpd
     * @Description 查询用户发布的所有动态
     * @Date 2023-04-17 下午 07:53
     * @Return com.zhuiYun.project.common.response.CommonResult

     */
    @Override
    public List<TrendContent> getUserTrends(Integer userId) {
        return trendContentMapper.getUserTrends(userId);
    }

    /**
     * @Author gpd
     * @Description 查询用户选择的动态内容
     * @Date 2023-04-18 上午 09:12
     * @Return com.zhuiYun.project.common.response.CommonResult

     */
    @Override
    public TrendContent getUserSelectTrend(Integer id,Integer userId) {

        return trendContentMapper.getUserSelectTrend(id,userId);
    }

    /**
     * @Author gpd
     * @Description 动态评论点赞
     * @Date 2023-04-19 上午 10:31
     * @Return com.zhuiYun.project.common.response.CommonResult

     */
    @Override
    public Integer thumbsCommentUpState(TrendCommentThumb trendCommentThumb) {
        Integer tId = trendCommentThumb.getTcId();
        trendCommentMapper.thumbsCommentUpState(tId);
        return trendCommentThumbMapper.insertSelective(trendCommentThumb);
    }

    /**
     * @Author gpd
     * @Description 动态评论取消点赞
     * @Date 2023-04-19 上午 10:45
     * @Return com.zhuiYun.project.common.response.CommonResult

     */
    @Override
    public Integer noThumbsCommentUpState(TrendCommentThumb trendCommentThumb) {
        trendCommentMapper.noThumbsCommentUpState(trendCommentThumb.getTcId());
        TrendCommentThumb trendThumb1 = trendCommentThumbMapper.selectOne(trendCommentThumb);
        return trendCommentThumbMapper.delete(trendThumb1);
    }

    /**
     * @Author gpd
     * @Description 查询用户发布的所有动态
     * @Date 2023-04-19 下午 04:16
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param userId
     */
    @Override
    public List<TrendContent> getToUserThrend(Integer userId) {
        TrendContent trendContent = new TrendContent();
        trendContent.setUserId(userId);
        trendContent.setDelFlag(0);
        return trendContentMapper.select(trendContent);
    }

    /**
     * @Author gpd
     * @Description 删除用户动态
     * @Date 2023-04-19 下午 04:34
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param id
     */
    @Override
    public Integer delUserTrend(Integer id) {
        TrendContent trendContent = new TrendContent();
        trendContent.setId(id);
        trendContent.setDelFlag(1);
        return trendContentMapper.updateByPrimaryKeySelective(trendContent);
    }

    /**
     * @Author gpd
     * @Description 修改动态内容状态 公开 （uniapp我的动态内容）
     * @Date 2023-04-20 上午 09:34
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param id
     */
    @Override
    public Integer openTrend(Integer id) {
        TrendContent trendContent = new TrendContent();
        trendContent.setId(id);
        trendContent.setPublishStatus(0);
        return trendContentMapper.updateByPrimaryKeySelective(trendContent);
    }

    /**
     * @Author gpd
     * @Description 修改动态内容状态 私密uniapp我的动态内容）
     * * @Date 2023-04-20 上午 09:34
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param id

     */
    @Override
    public Integer privateTrend(Integer id) {
        TrendContent trendContent = new TrendContent();
        trendContent.setId(id);
        trendContent.setPublishStatus(1);
        return trendContentMapper.updateByPrimaryKeySelective(trendContent);
    }


    /**
    * @Author gpd
    * @Description 查询用户评论的递归 （uniapp用户动态）
    * @Date 2023-04-10 下午 01:47
    * @Return TrendComment

    */
    private TrendComment getChild(TrendComment trendComment, List<TrendComment> tr) {
        List<TrendComment> newList = new ArrayList<>();
        for (TrendComment comment : tr){
            if(trendComment.getId() == comment.getParentId()){
                newList.add(getChild(comment,tr));
            }
        }
        trendComment.setChilds(newList);
        return trendComment;
    }



}
